<template>
    <div class="headA">
        <loading :props="{type:'page' ,show:true}"></loading>
    </div>
</template>
<script>
import loading from './Loading'
import md5 from 'md5'
import * as API from '../api'
export default {
    name: 'checkPayStatus',
    components: {
        loading
    },

    data: function() {
        return {
            userInfoAjax: true,
            isGuest: true,
        }
    },

    activated: function() {
        this.isGuest = parseInt(this.$store.state.user.userInfo.status.isGuest) == 1 ? true : false;
        this.checkPayStatus();
    },

    methods: {
        checkPayStatus: function() {
            let tthis = this,
                keyVal = tthis.$route.params.key,
                keyRe = /^[0-9a-z]{32}$/,
                cur_url = Unit.getSessionStorage('cur_url'),
                keyFormat = new RegExp(keyRe);
            if (keyFormat.test(keyVal)) {
                let userObj = tthis.$store.state.user.userInfo;
                if (keyVal == md5(userObj.account.usr_id + userObj.account.username + 'paid')) {
                    if (tthis.userInfoAjax) {
                        tthis.userInfoAjax = false;
                        tthis.shareGetAjaxUserInfo(function() {
                            Unit.setSessionStorage("paying", 'false');
                            if (tthis.isGuest) {
                                window.setTimeout(function() {
                                    tthis.getAjaxUserInfo('payok');
                                }, 20000);
                            }
                            if (cur_url != 'undefined' && cur_url != null) {
                                tthis.$router.push(cur_url);
                            } else {
                                tthis.$router.push('/browse');
                            }
                        }, function() { tthis.userInfoAjax = true });
                    }
                } else {
                    tthis.$router.push(cur_url);
                }
            } else if (Unit.getSessionStorage("paying") == 'true') {
                tthis.getAjaxUserInfo();
            } else {
                tthis.$router.push(cur_url);
            }
        },

        shareGetAjaxUserInfo: function(callback, completeBack) {
            var tthis = this;
            API.user_profile({
                data: {},
                success: function(response) {
                    if (response.errcode == 121) {
                        tthis.$store.dispatch('user_loginOut', tthis);
                    } else {
                        tthis.$store.dispatch('user_cacheUserInfo', response);
                        if (callback) {
                            callback();
                        }
                    }
                },
                complete: function() {
                    completeBack();
                }
            });
        },

        getAjaxUserInfo: function(type) {
            let tthis = this,
                cur_url = Unit.getSessionStorage('cur_url');
            if (tthis.userInfoAjax) {
                tthis.userInfoAjax = false;
                tthis.shareGetAjaxUserInfo(function() {
                    Unit.setSessionStorage("paying", 'false');
                    if (tthis.isGuest) {
                        if (type == 'payok') {
                            tthis.$store.dispatch('user_loginOut', tthis);
                            return;
                        }
                    }
                    if (cur_url != 'undefined' && cur_url != null) {
                        tthis.$router.push(cur_url);
                    } else {
                        tthis.$router.push('/browse');
                    }
                }, function() { tthis.userInfoAjax = true });
            }
        },

    },

}

</script>
<style scoped>
.headA {
    background-color: #fff;
    height: 44px;
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    z-index: 1;
}

</style>
